home *** CD-ROM | disk | FTP | other *** search
/ Loadstar 10 / 010.d81 / dos #25 < prev    next >
Text File  |  2022-08-26  |  3KB  |  185 lines

  1.  
  2.      DOS and don'ts -- Part 25
  3.           by Jimmy Weiler
  4.  
  5.  
  6.  
  7.   Still confused?  Let's graph it out.
  8.  
  9. Here's our file after we write two
  10.  
  11. names into it.
  12.  
  13.  
  14.      {CBM-A} Byte 1    {CBM-A} Byte 13
  15.      !           !
  16.      V           V
  17. {CBM-A}--->SCHLABOTNIK/8687247/ <68 nulls>
  18. ! {CBM-A}->WELLS/      6363088/ <68 nulls>
  19. ! !
  20. ! {CBM-Z}- Record 2
  21. {CBM-Z}--- Record 1
  22.  
  23.  
  24.   Each record has two fields, the name
  25.  
  26. and the phone number, separated by
  27.  
  28. carriage returns and padded out to the
  29.  
  30. next field with CHR$(0).  The rest of
  31.  
  32. the 89 characters in each record are
  33.  
  34. CHR$(0).  The first PRINT# in the
  35.  
  36. first record started at character 1.
  37.  
  38. "SCHLABOTNIK" is 11 letters long, plus
  39.  
  40. one carriage return, for 12 letters.
  41.  
  42. The second PRINT# in record 1 started
  43.  
  44. where we told it to--at character 13.
  45.  
  46.   When we wrote the second record,
  47.  
  48. the first PRINT# started at the first
  49.  
  50. character and took 6 characters to
  51.  
  52. finish.  The second PRINT# started at
  53.  
  54. character 13, so there are 6 null
  55.  
  56. characters between the end of the
  57.  
  58. first field and the start of the
  59.  
  60. second.  These nulls will have no
  61.  
  62. effect when we read them back, so
  63.  
  64. don't worry about them.
  65.  
  66.   Doing a POSITION every time you use
  67.  
  68. PRINT# gets old very quickly, and it
  69.  
  70. consumes valuable computer time, and
  71.  
  72. it uses extra memory space -- in other
  73.  
  74. words, there's an easier way to write
  75.  
  76. the fields into a RELative file's
  77.  
  78. record.
  79.  
  80.   Start, as before, by POSITIONing
  81.  
  82. to the start of the record:
  83.  
  84. 100 PRINT#15,"P"CHR$(4)CHR$(1)CHR$(0)
  85.     CHR$(1)
  86.  
  87. Then print all the fields at once, in
  88.  
  89. a single PRINT# statement:
  90.  
  91.  
  92. 200 PRINT#3,"SCHLABOTNIK"CHR$(13)
  93.     "8687247"
  94.  
  95.  
  96. Of course, in most programs, all the
  97.  
  98. fields would already be contained in
  99.  
  100. strings, and the code would look more
  101.  
  102. like this:
  103.  
  104.  
  105. 150 CR$ = CHR$(13)
  106. 200 PRINT#3,NA$ CR$ PH$
  107.  
  108.  
  109.   One drawback of this technique is
  110.  
  111. that you never know just where in a
  112.  
  113. record any of the fields (except the
  114.  
  115. first) can be found.  This is no
  116.  
  117. problem if you intend always to read
  118.  
  119. or write a whole record, but if you
  120.  
  121. ever want to read just one field from
  122.  
  123. the record, or update just part of a
  124.  
  125. record, you will need to know exactly
  126.  
  127. what byte to POSITION to.
  128.  
  129.   One of the major advantages of REL
  130.  
  131. files over SEQ files is that you CAN
  132.  
  133. update a small piece of a REL file
  134.  
  135. without having any effect on the rest
  136.  
  137. of the file.  But take care!  Every
  138.  
  139. time you PRINT# into a record of a
  140.  
  141. RELative file, the entire contents of
  142.  
  143. that record from that point on will be
  144.  
  145. erased.
  146.  
  147.  
  148.   Here's an example:  Your record has
  149.  
  150. this in it:
  151.  
  152.  
  153. WILLOW/PINE/  MAPLE/ BIRCH/
  154.  
  155.  
  156. You want to replace PINE with OAK, so
  157.  
  158. you :
  159.  
  160.  
  161. PRINT#15,"P"CHR$(4)CHR$(LB)CHR$(HB)
  162. CHR$(8):PRINT#3,"OAK"
  163.  
  164.  
  165. Now your record has:
  166.  
  167.  
  168. WILLOW/OAK/
  169.  
  170.  
  171. -- obviously not what you intended.
  172.  
  173. The safest way to update a record is
  174.  
  175. to read the entire record, change the
  176.  
  177. values in the appropriate variables,
  178.  
  179. and write the whole thing back onto
  180.  
  181. the disk.
  182.  
  183.  
  184. -------- continued in Part 26 --------
  185.